<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<sc-upload-file :apiObj="this.$API.system.sysResource.uploadSysResource" v-model="fileList" style="margin:0px 10px 0px 10px;"
								:on-success="handleSuccess"
								:show-file-list="false">
					<el-button type="warning" icon="sc-icon-upload">创建</el-button>
				</sc-upload-file>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			</div>
			<div class="right-panel">
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :data="tableData" row-key="fileName" @selection-change="selectionChange" stripe remoteSort remoteFilter>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="资源名称" prop="fileName" width="200" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="文件格式" prop="extName" width="100"></el-table-column>
				<el-table-column label="文件大小" prop="fileSize" width="100">
					<template #default="scope">
						{{(scope.row.fileSize/(1024*1024)).toFixed(2)}}M
					</template>
				</el-table-column>
				<el-table-column label="引用路径" prop="url" width="400"></el-table-column>
				<el-table-column label="创建时间" prop="createTime" width="150"></el-table-column>
				<el-table-column label="修改时间" prop="lastModifiedTime" width="150"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="120">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="previewFile(scope.row, scope.$index)">预览</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<preview-dialog v-if="dialog.previewSave" ref="previewSaveDialog"  @closed="dialog.previewSave=false"></preview-dialog>
</template>

<script>
import  previewDialog from './preview'
export default {
	name: 'Sys Resource Manage',
	components: {
		previewDialog
	},
	data() {
		return {
			dialog: {
				previewSave:false
			},
			tableData:[],
			selection:[],
			fileList:[],
		}
	},
	mounted() {
		this.getResourceFileList();
	},
	methods: {
		async getResourceFileList()
		{
			var res = await this.$API.system.sysResource.getResourceFileList.get();
			if (res.code == 200) {
				this.tableData = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		previewFile(row,index) {
			this.dialog.previewSave = true
			this.$nextTick(() => {
				this.$refs.previewSaveDialog.open().setData(row);
			})
		},
		//删除
		async table_del(row) {
			var reqData = {fileName: row.fileName}
			var res = await this.$API.system.sysResource.deleteSysResource.post(reqData);
			if (res.code == 200) {
				this.$message.success("删除成功")
				this.getResourceFileList();
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var fileNameArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					fileNameArr.push(this.selection[i].fileName);
				}
				const loading = this.$loading();
				var res = await this.$API.system.sysResource.deleteSysResourceByFileNames.post({fileNames: fileNameArr});
				if (res.code == 200) {
					this.$message.success(res.message)
					this.getResourceFileList();
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//本地更新数据
		handleSuccess(){
			this.getResourceFileList();
		},
	}
}
</script>

<style>
</style>
